package com.example.task1.ui.AllService.movies;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.MoniesFilmBean;
import com.example.task1.ui.BaseAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MoviesFilmAdapter extends BaseAdapter<MoniesFilmBean.RowsDTO> {

    private Context context;
    private List<MoniesFilmBean.RowsDTO> data = new ArrayList<>();
    private ImageView image;
    private TextView tvTitle;
    private TextView tvScore;
    private TextView tvLanguage;
    private TextView tvDuration;
    private TextView tvPlayDate;
    private TextView tvIntroduction;
    private Button btnUp;
    private int id;

    public MoviesFilmAdapter(Context context, int id) {
        this.context = context;
        this.id = id;
    }

    @Override
    protected void setData(List<MoniesFilmBean.RowsDTO> rows) {
        int size = data.size();
        data.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.moviescinemalist;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected List<MoniesFilmBean.RowsDTO> getData() {
        return data;
    }

    @Override
    protected void onBind(myHolder holder, MoniesFilmBean.RowsDTO date) {
        MyApplication.glide(image, date.getCover());
        tvTitle.setText(date.getName());
        tvScore.setText("评分：" + date.getScore());
        tvLanguage.setText("语言：" + date.getLanguage());
        tvDuration.setText("时长：" + date.getDuration());
        tvPlayDate.setText("上映时间：" + date.getPlayDate());
        tvIntroduction.setText("简介：" + Html.fromHtml(date.getIntroduction()));
        btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (id == 0) {
                    MyApplication.intent(context, MoviesCinemaActivity.class);
                    MyApplication.show("还没有选择影院");
                } else {
                    Intent intent = new Intent(context, MoviesCinemaDetailsActivity.class);
                    intent.putExtra("id", id);
                    intent.putExtra("ids", date.getId());
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View itemView) {
        image = (ImageView) itemView.findViewById(R.id.image);
        tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        tvScore = (TextView) itemView.findViewById(R.id.tv_score);
        tvLanguage = (TextView) itemView.findViewById(R.id.tv_language);
        tvDuration = (TextView) itemView.findViewById(R.id.tv_duration);
        tvPlayDate = (TextView) itemView.findViewById(R.id.tv_playDate);
        tvIntroduction = (TextView) itemView.findViewById(R.id.tv_introduction);
        btnUp = itemView.findViewById(R.id.btn_ups);
    }
}
